<template>
  <el-container class="home_container">
    <!-- 头部区域 -->
    <el-header>
      <div class="log_box">
        <img src="@/assets/logo.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">注销</el-button></el-header
    >
    <el-container>
      <!-- 左侧边栏区域 -->
      <el-aside :width="menusWidth + 'px'">
        <!-- // 隐藏菜单 -->
        <button class="hide_menus" @click="collapseMenus">
          <i :class="collapseIcon"></i>
        </button>
        <!-- 菜单 -->
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#3572F3" unique-opened :default-active="actived_item" :collapse="collapseStatus" :collapse-transition="false" router>
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menusList" :key="item.id">
            <!-- 标题和图标 -->
            <template slot="title">
              <i :class="menusIcons[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="activeItem(subItem.path)">
              <!-- 标题和图标 -->
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <!-- 主体区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data() {
    return {
      // 菜单
      menusList: [],
      // 菜单图标
      menusIcons: {
        125: 'el-icon-s-custom',
        103: 'el-icon-s-check',
        101: 'el-icon-s-goods',
        102: 'el-icon-s-claim',
        145: 'el-icon-s-data'
      },
      // 当前激活的菜单项
      actived_item: '',
      // 隐藏显示菜单栏
      collapseIcon: 'el-icon-s-fold',
      collapseStatus: false,
      menusWidth: 200
    }
  },
  created() {
    // 获取菜单
    this.getMenus()
    // 读取当前激活的菜单
    this.actived_item = window.sessionStorage.getItem('actived')
  },
  methods: {
    // 注销
    logout() {
      // 清空token
      window.sessionStorage.clear()
      //
      this.$router.push('/login')
    },
    // 获取菜单列表
    async getMenus() {
      const { data: res } = await this.$http.get('menus')
      // console.log(res)
      if (res.meta.status !== 200) {
        this.$message.error('获取菜单失败')
      }

      this.menusList = res.data
    },
    // 修改当前激活菜单项
    activeItem(id) {
      this.actived_item = '' + id
      window.sessionStorage.setItem('actived', this.actived_item)
    },
    // 折叠菜单栏
    collapseMenus() {
      if (this.collapseStatus) {
        this.collapseStatus = false
        this.collapseIcon = 'el-icon-s-fold'
        this.menusWidth = 200
      } else {
        this.collapseStatus = true
        this.collapseIcon = 'el-icon-s-unfold'
        this.menusWidth = 66
      }
    }
  }
}
</script>

<style lang="less" scoped>
.home_container {
  height: 100%;
  // 头部样式区
  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #fff;
    .log_box {
      height: 80%;
      display: flex;
      align-items: center;
      font-size: 25px;

      img {
        height: 100%;
        margin-right: 15px;
      }
    }
  }
  .el-aside {
    background-color: #333744;
    .hide_menus {
      width: 100%;
      font-size: 20px;
    }
    .el-menu {
      border-right: none !important;
    }
  }
  .el-main {
    padding: 0;
    margin: 0;
  }
}
</style>
